package com.intexh.quickandroid.module.chat.ui.face;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.intexh.quickandroid.R;


/**
 * Created by roger on 4/25/14.
 */
public class EmojiAdapter extends BaseAdapter {
    Context mContext;
    private int startResId;
    private int count;

    public EmojiAdapter(Context context, int startResId, int count)
    {
        this.mContext = context;
        this.startResId = startResId;
        this.count = count;
    }

    @Override
    public int getCount()
    {
        return count + 1;
    }

    @Override
    public Object getItem(int i)
    {
        int resIndex = startResId + i;
        int lastItem = startResId + count;

        if (resIndex == lastItem)
        {
            return EmojiPageAdapter.DELETE_CODE;
        }
        else if (resIndex >= EmojiFactory.codePoints.length)
        {
            return null;
        }
        else if (resIndex < lastItem)
        {
            return EmojiFactory.codePoints[resIndex];
        }

        return null;
    }

    @Override
    public long getItemId(int i)
    {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        String code = (String)getItem(i);
        try {
        	  if (EmojiPageAdapter.DELETE_CODE.equals(code))
              {	
                  imageView.setImageResource(R.drawable.emoji_delete);
              }
              else if (code != null)
              {
                  imageView.setImageDrawable(EmojiFactory.getDrawable(mContext, code));
              }
        	
		} catch (OutOfMemoryError oom) {
		   oom.printStackTrace();
		   System.gc();
		}
        
        return imageView;
    }
}
